package com.edou.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.system.dao.ISysUploadFileDao;
import com.edou.system.dto.SysUploadFileCondition;
import com.edou.system.entity.SysUploadFile;
import com.edou.system.service.ISysUploadFileService;

@Service("sysUploadFileServiceImpl")
@Transactional(value = "transactionManager")
public class SysUploadFileServiceImpl implements ISysUploadFileService{

	private static final Log logger = LogFactory.getLog(SysUploadFileServiceImpl.class); 
	
	//云存储域名
	@Value("${cloud.storage.doamin}")
	private String cloudStorageDomain;
	//云存储账号
	@Value("${cloud.storage.account.name}")
	private String cloudStorageAccountName;
	//云存储key
	@Value("${cloud.storage.account.key}")
	private String cloudStorageAccountKey;
	
	@Resource(name = "sysUploadFileDaoImpl")
	private ISysUploadFileDao sysUploadFileDao;

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listSysUploadFileByPage(PageResult<SysUploadFile> pageResult,SysUploadFileCondition condition){
		int rows = sysUploadFileDao.listSysUploadFileByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<SysUploadFile> list = sysUploadFileDao.listSysUploadFileByPage(rowBounds,condition);
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 文件图片表
	 */
	public SysUploadFile getSysUploadFileById(String id){
		return sysUploadFileDao.getSysUploadFileById(id);
	}
	
	/**
	 * 根据URL查询文件实体
	 * @param url
	 * @return
	 */
	public SysUploadFile getSysUploadFileByUrl(String url){
		return sysUploadFileDao.getSysUploadFileByUrl(url);
	}
	
	/**
	 * 根据ID查询
	 * @param 相关联的对象ID
	 * @param 相关联的对象类型ID
	 */
	public List<SysUploadFile> listSysUploadFileByObjectIdAndTypeId(String objectId,String typeId){
		List<SysUploadFile> list = sysUploadFileDao.listSysUploadFileByObjectIdAndTypeId(objectId,typeId);
		return list;
	}
	
	/**
	 * 根据关联的对象ID集合查询
	 */
	public List<SysUploadFile> listSysUploadFileByIds(String[] ids){
		return sysUploadFileDao.listSysUploadFileByIds(ids);
	}

	/**
	 * 新增
	 * @param item 文件图片表
	 */
	public void add(SysUploadFile item){
		// 获取文件扩展名
		int lastIndex = item.getFileName().lastIndexOf(".");
		item.setExtend(item.getFileName().substring(lastIndex+1,item.getFileName().length()));
		item.setType(1);
		sysUploadFileDao.add(item);
	}

	/**
	 * 修改
	 * @param item 文件图片表
	 */
	public void update(SysUploadFile item){
		// 获取文件扩展名
		int lastIndex = item.getFileName().lastIndexOf(".");
		item.setExtend(item.getFileName().substring(lastIndex+1,item.getFileName().length()));
		item.setType(1);
		logger.info("==== 更新图片：" + item.getFileName());
		sysUploadFileDao.update(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		sysUploadFileDao.delByIds(ids);
	}

	/**
	 * 根据objectId与objectType更新location
	 * 使用于：
	 * 1、产品图片设置为缩略图时使用：ProductPhotoMemberController
	 * @param objectId
	 * @param objectType
	 */
	public void updateLocationByObjectIdType(Integer location, String objectId, Integer objectType) {
		sysUploadFileDao.updateLocationByObjectIdType(location, objectId, objectType);
	}

	/**
	 * 根据id更新location
	 * 使用于：
	 * 1、产品图片设置为缩略图时使用：ProductPhotoMemberController
	 * @param location
	 * @param id
	 */
	public void updateLocationById(Integer location, String id) {
		sysUploadFileDao.updateLocationById(location, id);
	}

	/**
	 * 根据条件查询文件
	 */
	@Override
	public List<SysUploadFile> listSysUploadFileByCondition(SysUploadFileCondition condition) {
		// TODO Auto-generated method stub
		return sysUploadFileDao.listSysUploadFileByCondition(condition);
	}

}

